import Vue from 'vue'
import App from './App.vue'
import router from './router'
//引入axios
import axios from 'axios'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import { Message } from 'element-ui';

import qs from 'qs'

Vue.prototype.$qs = qs

Vue.use(ElementUI);
//配置axios，定义原型
Vue.prototype.$http = axios
//设置axios的基础路径
Vue.prototype.$http.defaults.baseURL='http://localhost:8080'
//解决中文乱码问题，设置内容类型编码 x-www-form-urlencoded
// Vue.prototype.$http.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
//请求拦截器
Vue.prototype.$http.interceptors.request.use(
    req => {
      let token = localStorage.getItem("token");
      console.log("拦截请求token--->" + token);
      if (token) {
        //把localStorage的token放在Authorization里
          req.headers.Authorization = token;
      }
      return req;
    },
    err => {
      console.log("失败信息" + err);
    }
);
//响应拦截器
Vue.prototype.$http.interceptors.response.use(
    resp => {
        console.log('response拦截',resp)
        if(resp.data == "not login"){
            Message.error({message:'没有登录，请重新登录'})
            location.href = '/login'
        }
        return resp;
    },
    err => {
        console.log("失败信息" + err);
    }
);

Vue.config.productionTip = false

new Vue({
  router,
  render: function (h) { return h(App) }
}).$mount('#app')
